package Demo1299.sikao;

import java.io.FileWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.Properties;
/*
 * 1、Person代码不可以更改！！！
 *  创建一个Person对象，
 *  保存如下数据到对象中：
 *  小张，18,  130302201110010001
 * 调用Person对象的toString()方法在控制台展示数据
 *
 * 2、将Person对象的数据保存到data2.properties文件中（字段顺序无序即可）
 *    name=小张
 *    age=18
 *    idCardNum=130302201110010001
 */
public class Dmeo1 {
    public static void main(String[] args) throws Exception {
        Class<?> c = Class.forName("Demo1299.sikao.Person");
        Constructor<?> c2 = c.getDeclaredConstructor(String.class, int.class, String.class);
        c2.setAccessible(true);
        Object person = c2.newInstance("小张", 18, "130302201110010001");
        System.out.println(person.toString());
        Field name = c.getDeclaredField("name");
        name.setAccessible(true);
        Field age = c.getDeclaredField("age");
        age.setAccessible(true);
        Field idCardNum = c.getDeclaredField("idCardNum");
        idCardNum.setAccessible(true);
        Properties pro = new Properties();
        pro.setProperty("name",(String) name.get(person));
        pro.setProperty("age",(int)age.get(person)+"");
        pro.setProperty("idCardNum",(String) idCardNum.get(person));
        FileWriter file = new FileWriter("C:\\Daymate\\src\\Demo1299\\sikao\\data2.properties");
        pro.store(file,"=");
        file.close();
    }
}
